// components/chat-item/chat-item.ts
import {ChatItemType, ChatStatusEnum} from "../../types/index";
import common from "../../common/index";

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    chatItem: {
      type: Object<ChatItemType>,
      value: common.getDefChatItem()
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    ChatStatusEnum,
    showReference: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 显示参考资料悬浮层
    showReferenceModal() {
      this.setData({
        showReference: true
      });
    },

    // 隐藏参考资料悬浮层
    hideReferenceModal() {
      this.setData({
        showReference: false
      });
    },

    // 阻止事件冒泡
    preventHide() {
      // 空函数，阻止点击悬浮层内容时关闭
    },

    // 参考资料点击事件 - 显示内容并复制链接
    onReferenceClick(e: any) {
      const reference = e.currentTarget.dataset.reference;
      const link = reference.link || reference.url;
      const content = reference.content || reference.ai_abstract || '暂无详细内容';
      const title = reference.ai_title || '参考资料';
      
      // 显示内容
      wx.showModal({
        title: title,
        content: content,
        showCancel: true,
        cancelText: '关闭',
        confirmText: '复制链接',
        success: (res) => {
          if (res.confirm && link) {
            // 复制链接
            wx.setClipboardData({
              data: link,
              success: () => {
                wx.showToast({
                  title: '链接已复制',
                  icon: 'success',
                  duration: 1500
                });
              },
              fail: () => {
                wx.showToast({
                  title: '复制失败',
                  icon: 'none',
                  duration: 1500
                });
              }
            });
          }
        }
      });
    },

    // 论文卡片点击事件
    onPaperClick(e: any) {
      const paper = e.currentTarget.dataset.paper;
      const content = paper.abstract || '暂无摘要';
      
      wx.showModal({
        title: paper.title,
        content: content,
        showCancel: true,
        cancelText: '关闭',
        confirmText: '复制链接',
        success: (res) => {
          if (res.confirm && paper.url) {
            wx.setClipboardData({
              data: paper.url,
              success: () => {
                wx.showToast({
                  title: '链接已复制',
                  icon: 'success',
                  duration: 1500
                });
              }
            });
          }
        }
      });
    },

    // 打开论文链接
    openPaperUrl(e: any) {
      const url = e.currentTarget.dataset.url;
      if (url) {
        wx.setClipboardData({
          data: url,
          success: () => {
            wx.showToast({
              title: '链接已复制',
              icon: 'success'
            });
          }
        });
      }
    }
  }
})
